package com.test.utilities;

import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.http.NameValuePair;
import org.json.JSONObject;

import com.test.followme.Globals;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.location.LocationManager;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.NetworkInfo.State;
import android.widget.Toast;

public class Utility
{
	public static void addSharedPrefs(Context context, String tag, String data)
	{
		if (isNullOrEmpty(data))
		{
			return;
		}
		final SharedPreferences prefs = getGcmPreferences(context);
		SharedPreferences.Editor editor = prefs.edit();
		editor.putString(tag, data);
		// alp:: splash te bu eklenecek
		// editor.putInt(Globals.PROPERTY_APP_VERSION, appVersion);
		editor.commit();
	}
	
	private static SharedPreferences getGcmPreferences(Context context)
	{
		return context.getSharedPreferences(Globals.PREFS_NAME, Context.MODE_PRIVATE);
	}
	
	public static String getSharedPrefs(Context context, String tag)
	{
		final SharedPreferences prefs = getGcmPreferences(context);
		String data = prefs.getString(tag, "");
		if (data == null || data.equals(""))
		{
			return "";
		}
		return data;
	}
	
	public static JSONObject httpRequest(List<NameValuePair> params)
	{
		JSONParser jsonParser = new JSONParser();
		
		JSONObject json = jsonParser.getJSONFromUrl(Globals.SERVER_URL, params);
		
		return json;
	}
	
	public static boolean isValidEmail(String email)
	{
		Pattern pattern = Pattern.compile(Globals.EMAIL_PATTERN);
		Matcher matcher = pattern.matcher(email);
		return matcher.matches();
	}
	
	public static boolean isValidPassword(String pass)
	{
		if (!isNullOrEmpty(pass) && pass.length() >= 6 && pass.length() <= 12)
		{
			return true;
		}
		return false;
	}
	
	public static boolean isNullOrEmpty(String str)
	{
		if (str == null || str.equals(""))
		{
			return true;
		}
		return false;
	}
	
	public static void checkGps(Context context, LocationManager locationManager)
	{
		if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER))
		{
			Toast.makeText(context, "GPS is Enabled in your device", Toast.LENGTH_SHORT).show();
		}
		else
		{
			showGPSDisabledAlert(context);
		}
	}
	
	public static void showGPSDisabledAlert(final Context context)
	{
		AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(context);
		alertDialogBuilder.setMessage("GPS is disabled in your device. Would you like to enable it?").setCancelable(false).setPositiveButton("Goto Settings Page To Enable GPS", new DialogInterface.OnClickListener()
		{
			public void onClick(DialogInterface dialog, int id)
			{
				Intent callGPSSettingIntent = new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS);
				context.startActivity(callGPSSettingIntent);
			}
		});
		
		alertDialogBuilder.setNegativeButton("Cancel", new DialogInterface.OnClickListener()
		{
			public void onClick(DialogInterface dialog, int id)
			{
				dialog.cancel();
			}
		});
		
		AlertDialog alert = alertDialogBuilder.create();
		alert.show();
	}
	
	public static void checkInternet(Context context)
	{
		ConnectivityManager connect = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
		State mobile = connect.getNetworkInfo(0).getState();
		State wifi = connect.getNetworkInfo(1).getState();
		
		if (wifi == NetworkInfo.State.CONNECTED || wifi == NetworkInfo.State.CONNECTING)
		{
			Toast.makeText(context, "WiFi Connected", Toast.LENGTH_SHORT).show();
		}
		else if (mobile == NetworkInfo.State.CONNECTED || mobile == NetworkInfo.State.CONNECTING)
		{
			Toast.makeText(context, "Mobile Connected", Toast.LENGTH_SHORT).show();
		}
		else
		{
			Toast.makeText(context, "No Internet", Toast.LENGTH_SHORT).show();
		}
		
	}
	
	public static void exit(Activity act)
	{
		Intent intent = new Intent(Intent.ACTION_MAIN);
		intent.addCategory(Intent.CATEGORY_HOME);
		intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		act.startActivity(intent);
	}
}
